package com.bidevalution.support.context;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.http.HttpStatus;

import com.bidevalution.support.factory.WebSocketOpType;
import com.qtp.core.util.GsonUtil;

public class ResponseObject extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	private static final Integer SUCCESS_STATUS = 200;
	private static final Integer ERROR_STATUS = -1;
	private static final String SUCCESS_MSG = "一切正常";

	public ResponseObject() {
	}
	public ResponseObject(WebSocketOpType webSocketOpType) {
		super();
		put("opType", webSocketOpType==null?"":webSocketOpType.name());
		put("timestamp", new Date().getTime());
	}

	public ResponseObject(String accessToken,WebSocketOpType webSocketOpType,int status) {
		super();
		setStatus(status);
		put("opType", webSocketOpType==null?"":webSocketOpType.name());
		put("timestamp", new Date().getTime());
	}

	public ResponseObject(String accessToken,WebSocketOpType webSocketOpType,HttpStatus status) {
		super();
		setStatus(status.value());
		setMsg(status.getReasonPhrase());
		put("opType", webSocketOpType==null?"":webSocketOpType.name());
		put("timestamp", new Date().getTime());
	}

	public ResponseObject success() {
		setMsg(SUCCESS_MSG);
		return this;
	}

	public ResponseObject success(String msg) {
		put("msg", msg);
		return this;
	}

	public ResponseObject error(String msg) {
		setStatus(ERROR_STATUS);
		put("msg", msg);
		setStatus(ERROR_STATUS);
		return this;
	}

	public ResponseObject setData(String key, Object obj) {
		@SuppressWarnings("unchecked")
		HashMap<String, Object> data = (HashMap<String, Object>) get("data");
		if (data == null) {
			data = new HashMap<String, Object>();
			put("data", data);
		}
		data.put(key, obj);
		return this;
	}

	public ResponseObject setStatus(int status) {
		put("status", status);
		return this;
	}

	public ResponseObject setMsg(String msg) {
		put("msg", msg);
		return this;
	}

	public ResponseObject setValue(String key, Object val) {
		put(key, val);
		return this;
	}

	/**
	 * 设置需要客户端反馈的列表，用于
	 * @param todoList
	 * @return
	 */
	public ResponseObject setTodoList(List<String[]> todoList) {
		put("todoList", todoList);
		return this;
	}

	public ResponseObject setMd5(String md5) {
		put("md5", md5);
		return this;
	}

	public ResponseObject setReceiveKey(String receiveKey) {
		put("receiveKey", receiveKey);
		return this;
	}

	public ResponseObject delTodoList() {
		remove("todoList");
		return this;
	}

	/**
	 *  获取客户端反馈列表
	 * @return
	 */
	public List<String[]> getTodoList() {
		return (List<String[]>) get("todoList");
	}

	public String getOpType() {
		return (String) get("opType");
	}

	public Long getTimestamp() {
		return (Long) get("timestamp");
	}

	public ResponseObject setTimestamp(Long timestamp) {
		put("timestamp", timestamp);
		return this;
	}

	/**
	 * 返回JSON字符串
	 */
	@Override
	public String toString() {
		return GsonUtil.toJson(this);
	}

}
